<?php
// This requires the PHP Powertab parser
require_once $_SERVER['DOCUMENT_ROOT']."/lib/php-ptparser/powertabdocument.php";
// This requires the pte2tex parser
require_once "pte2tex.php";
// Create a new instance of the parser
$pt2tex = new pte2tex();

//Open a new document
$ptb = new PowerTabDocument ();
/*
 * CURRENTLY THERE IS NO ERROR CHECKING...
 */
$filename = $_SERVER['DOCUMENT_ROOT']."/tests/powertabs/Something Great Disappears.ptb";
$ptb->Load($filename); // Open and Load Power Tab
//Print the header
$header = &$ptb->GetHeader();
$pt2tex->writeSongInfo(&$header);

//We're going to do the guitar score
$guitarScore = &$ptb->GetGuitarScore();

// Define the tuning and capo etc. do this just for the first guitar at the moment
$guitar = &$guitarScore->GetGuitar(0);
$pt2tex->writeGuitarInfo(&$guitar);
/*
 * A key flaw here is that it assumes that the first guitar plays the first
 * stave for the entire length of the song. This would need to be fixed by
 * modifying alphaTab and making \instrument a beat effect.
 */


// After the header information a line with a '.' must be printed
printf(".<br>\n");


//Iterate through the systems
for($sysCount=0;$sysCount < $guitarScore->GetSystemCount();$sysCount++){
	$system = &$guitarScore->GetSystem($sysCount);
	//For the minute as alphatab doesn't like multiple staves, just get the
	//Check if the stave exists
	$staffNo = 0;
	if($system->GetStaffCount() <= $staffNo){
		//No stave here
		continue;
	}
	$staff = &$system->GetStaff($staffNo);
	//Get the positions of the barlines
	$barlines = array();//Contains the type of the bar like "| \ro"
	$barlinePos = array();//Contains the positions of the bars
	//Push the first bar onto it
	$pt2tex->parseBarline(&$barlines,$system->GetStartBar());
	//Push the bars that are during the stave
	for($k = 0;$k < $system->GetBarlineCount();$k++){
		$barline = $system->GetBarline($k);
		array_push($barlinePos,$barline->GetPosition());
		$pt2tex->parseBarline(&$barlines,$barline);
	}
	//Do the end bar
	$pt2tex->parseBarline($barlines,$system->GetEndBar());
	//Do the staff in each system
	$k = 0;// This counts the barline we're up to
	printf("%s <br>\n",$barlines[$k++]);//Print the first barline in the system
	//Print the clef for this stave
	printf("\\clef %s ",$staff->GetClefName());
	//$posCount = The position counter this includes spaces and bars
	//$notePos  = The Note counter, this counts how many notes have been displayed
	for($posCount=0,$notePos=0;$notePos < $staff->GetPositionCount(0);$posCount++){
		$position = $staff->GetPosition(0,$notePos);
		
		//Check If the position doesn't contain a note:
		//This is when the note position is not equal to $posCount
		if($posCount !== $position->GetPosition(0,$notePos)){
			//Check if a bar
			if(in_array($posCount,$barlinePos)){
				//Check what kind of bar it is
				printf("%s <br>\n",$barlines[$k++]);
			}else{
				//Check for tempo change
				//Print a space, even though alphaTab will ignore them
				//If laid out with two staves this would help
				printf(" ");
			}
			//The only thing we need to check for is a tempo change
			//If it's nothing else than do nothing! it's a space!
			//Add $posCount but don't add notePos
			continue;
		}
		
		$noteNo = $position->GetNoteCount();
		//If it's a rest
		if($position->IsRest()){
			printf("r.%d ",$position->getDurationType());
			$notePos++;
			continue;
		}
		if($position->IsAcciaccatura()){
			//Don't output anything
			for($i = 0;$i< $position->GetNoteCount();$i++){
				$note = $position->GetNote($i);
				$pt2tex->IsAcciaccatura(&$position,&$note);
			}
		}else if($position->IsRest()){
			//Write the rest and possible beat effects (like dot or double dot)
			printf("r.%s%d ",$position->writeBeatFX($position),$position->getDurationType());
		}else{
			//It's a note
			//If there is more than one note
			if($noteNo > 1){
				printf("(");
			}
			for($i = 0;$i< $position->GetNoteCount();$i++){
				$note = $position->GetNote($i);

				if($note->IsTied()){
					printf("-.%d",$note->GetString()+1);
				}else if($note->IsMuted()){
					printf("x.%d",$note->GetString()+1);
				}else{
					printf("%d.%d",$note->GetFretNumber(),$note->GetString()+1);
				}
				//Write beat / note effects
				$pt2tex->writeFX($position,$note);
				if($i < $noteNo-1){
					printf(" ");
				}
			}
			if($noteNo > 1){
				printf(")");
			}
			printf(".%d",$position->GetDurationType());
		}
		
		
		
		printf(" ");
		$notePos++;
	}
}




?>